﻿using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Aggregation.ValueObject.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Synthesize.BaseSet.Domain.DomainService.Authority
{

    /// <summary>
    /// 菜单移动逻辑
    /// </summary>
    public class MenuMoveDomainService
    {
        private readonly IMenuRepository _menuRepository;

        public MenuMoveDomainService(IMenuRepository menuRepository)
        {
            _menuRepository = menuRepository;
        }

        /// <summary>
        /// 移动菜单
        /// </summary>
        /// <param name="platform"></param>
        /// <param name="id"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public async Task<bool> MoveMenu(MenuPlatformEnum platform,Guid id, int flag)
        {
            //（1）所有功能
            var menuInfos = await _menuRepository.GetManyAsync(x => x.Platform == platform);
            //（2）当前功能 
            var currAdminFun = menuInfos.FirstOrDefault(x => x.Id == id);
            //（3）当前功能兄弟节点
            var brothersAdminFun = menuInfos.FindAll(x => x.ParentId == currAdminFun.ParentId);

            MenuInfoEntity broAdminFun = null;
            //上移
            if (flag == 1){
                broAdminFun = brothersAdminFun.Where(x => x.Sort < currAdminFun.Sort).OrderByDescending(x => x.Sort).FirstOrDefault();
            }
            else//下移
            {
                broAdminFun = brothersAdminFun.Where(x => x.Sort > currAdminFun.Sort).OrderBy(x => x.Sort).FirstOrDefault();
            }

            var currFuns = new List<MenuInfoEntity>();
            currFuns.Add(currAdminFun);
            GetMoveNode(menuInfos, currAdminFun.Id, currFuns);

            var moveFuns = new List<MenuInfoEntity>();
            if (broAdminFun!=null)
            {
                moveFuns.Add(broAdminFun);
                GetMoveNode(menuInfos, broAdminFun.Id, moveFuns);
            }
           
            //上移
            if (flag == 1)
            {
                foreach (var item in currFuns)
                {
                    item.Sort = item.Sort - moveFuns.Count;
                }
                foreach (var item in moveFuns)
                {
                    item.Sort = item.Sort + currFuns.Count;
                }
            }
            else
            {
                foreach (var item in currFuns)
                {
                    item.Sort = item.Sort + moveFuns.Count;
                }
                foreach (var item in moveFuns)
                {
                    item.Sort = item.Sort - currFuns.Count;
                }
            }

            _menuRepository.UpdateRange(currFuns);
            _menuRepository.UpdateRange(moveFuns);

            return true;
        }

        /// <summary>
        /// 获取子功能（递归）
        /// </summary>
        /// <param name="adminFuns"></param>
        /// <param name="funId"></param>
        /// <param name="move"></param>
        private void GetMoveNode(List<MenuInfoEntity> adminFuns, Guid funId, List<MenuInfoEntity> move)
        {
            List<MenuInfoEntity> borgs = adminFuns.FindAll(x => x.ParentId == funId);
            foreach (MenuInfoEntity item in borgs)
            {
                if (item.ParentId == funId)
                {
                    move.Add(item);
                    var tempFuns = adminFuns.FindAll(x => x.ParentId == item.Id);
                    if (tempFuns != null && tempFuns.Count > 0)
                    {
                        GetMoveNode(adminFuns, item.Id, move);
                    }
                }
            }
        }
    }
}
